﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace EPlatform_web.App_Start
{
    public class CrystalObject
    {
        public string sAttachPdfFileName;
        public string gRptLogId, gRptLogPwd;
        public string gErpRptPath;
        public string gExportPdfPath;
        public string sFormKey;
        public struct gRptFiles
        {
            public string RptFileName;
            public string FlowAttachName;
        }
        public struct gFlowAttachFiles
        {
            public string FileType;
            public string FileName;
            public string FilePath;
            public string FileDisplayName;
        }
        public Boolean CreatePdf(gRptFiles sRptFile, List<string> sRptParamName, List<string> sRptParamVal, List<gFlowAttachFiles> sListPdfFile)
        {
            ReportDocument cryRpt = new ReportDocument();
            gFlowAttachFiles crfile = new gFlowAttachFiles();
            string sDecryptPwd;

            //密碼解密
            //sDecryptPwd = Decrypt(sPwKey, gRptLogPwd);
            sDecryptPwd = gRptLogPwd;
            cryRpt.Load(gErpRptPath + sRptFile.RptFileName);
            //log in 處理
            cryRpt.SetDatabaseLogon(gRptLogId, sDecryptPwd);

            //Report parameters 
            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            for (int i = 0; i <= sRptParamName.Count - 1; i++)
            {
                //paramter
                crParameterDiscreteValue.Value = sRptParamVal[i];
                crParameterFieldDefinition = crParameterFieldDefinitions[sRptParamName[i]];
                crParameterValues = crParameterFieldDefinition.CurrentValues;
                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            }
            //Export PDF file name
            //sAttachPdfFileName = sRptFile + '-' + sFormKey.Replace("*", "#");
            //指定flow附件名稱處理
            if (string.IsNullOrEmpty(sRptFile.FlowAttachName))
            { sAttachPdfFileName = sRptFile.RptFileName + '-' + sFormKey.Replace("*", "#"); }
            else
            { sAttachPdfFileName = sRptFile.FlowAttachName; }
            //增加判斷實體檔案名稱格式是否不支援
            sAttachPdfFileName = ReplaceFileNameSpecialSign(sAttachPdfFileName);

            try
            {
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = gExportPdfPath + sAttachPdfFileName + ".pdf";
                CrExportOptions = cryRpt.ExportOptions;
                {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                }
                cryRpt.Export();
                crfile.FileType = "CR";
                crfile.FilePath = gExportPdfPath;
                crfile.FileName = sAttachPdfFileName + ".pdf";
                sListPdfFile.Add(crfile);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        private string ReplaceFileNameSpecialSign(string pValue)
        {
            return pValue.Replace("/", "-").Replace("<", "-").Replace(">", "-").Replace("?", "-").Replace(":", "-").Replace("\"", "-").Replace("/", "-").Replace("*", "-").Replace("|", "-").Replace(";", "-");
        }
    }
}